package com.springboot.demo.threadpool;

import java.util.concurrent.*;

public class ScheduledPoolTest {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch lock = new CountDownLatch(5);
        ScheduledExecutorService schedule = Executors.newScheduledThreadPool(3);
//只能执行一次
        schedule.schedule(() -> {
            System.out.println("schedule");
        }, 1, TimeUnit.SECONDS);
//        反复执行
        ScheduledFuture<?> schedule1 = schedule.scheduleAtFixedRate(() -> {
            System.out.println("schedule");
            lock.countDown();
            long count = lock.getCount();
            System.out.println(count);
        }, 5000, 100, TimeUnit.MILLISECONDS);
//        0或者超时后执行后续任务，只能执行一遍
        lock.await(1000, TimeUnit.MILLISECONDS);
//        long count = lock.getCount();
//        System.out.println(count);
////        schedule.shutdown();
        schedule1.cancel(true);

    }
}
